// pages/my/my.js
var validate = require('../../utils/validate.js');
var httpTool = require('../../utils/network.js');
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data:{
    userInfo: {},
    authFlag: true,
    phone: '',
    password: '',
    //openId: '',
    pwdFlag: true,
  },
  bindGetUserInfo: function (res) {
    var that = this
   // res.detail.userInfo.avator = res.detail.userInfo.avatarUrl
    app.globalData.userInfo = res.detail.userInfo
    this.data.userInfo = res.detail.userInfo
   // app.userData.encryptedData = res.detail.encryptedData
    //app.userData.iv = res.detail.iv
   // app.userData.avatarUrl = res.detail.userInfo.avatarUrl
   // app.userData.nickName = res.detail.userInfo.nickName
    wx.setStorageSync('fhUserInfo', app.globalData.userInfo)

    this.setData({
      authFlag: true
    })
    this.toCommit()
  },
  // 显示隐私条款点击事件
  showPrivacyPolicy: function () {
    util.navigateTo('../privacyPolicy/privacyPolicy?type=0')
  },
  toCommit: function () {
    var that = this;
    var password = this.data.password
    var phone = this.data.phone
   // var openId = this.data.openId
    if (phone.length == 0) {
      util.fxAlert('请输入用户名')
      return;
    }
    if (password.length == 0) {
      util.fxAlert('请输入密码')
      return;
    }
    var param = {
      username: phone,
      password: password,
      loginType: 1,
      //openId: openId,
    }
    console.log(param)
    httpTool.post({
      url: "v1/member/doLogin",
      busyText: "请求中...",
      data: param,
      callback: function (res) {
        httpTool.busyClose();
        console.log(res)
        if (res.entity.avator == '') {
          that.toRelog()
        } else {
          // 登录成功 存储用户信息
          res.entity.userId = res.entity.id;
          app.globalData.userInfo = res.entity
          app.userData.userId = res.entity.id
          wx.setStorage({
            key: 'login',
            data: param,
          })
          wx.setStorage({
            key: 'user',
            data: res.entity,
            success: function (res) {
              //处理跳转逻辑
              wx.switchTab({
                url: '../index/index'
              });
            }
          })
        }

      }
    })
  },
  toRelog: function () {
    var that = this;
    var password = this.data.password
    var phone = this.data.phone
    // var openId = this.data.openId
    if (phone.length == 0) {
      util.fxAlert('请输入用户名')
      return;
    }
    if (password.length == 0) {
      util.fxAlert('请输入密码')
      return;
    }
    var param = {
      username: phone,
      password: password,
      loginType: 1,
      //avator: this.data.userInfo.avator,
      nickName: this.data.userInfo.nickName,
      //openId: openId,
    }
    console.log(param)
    httpTool.post({
      url: "v1/member/doLogin",
      busyText: "请求中...",
      data: param,
      callback: function (res) {
        httpTool.busyClose();
        console.log(res)
        // 登录成功 存储vipType
        res.entity.userId = res.entity.id;
        app.globalData.userInfo = res.entity
        app.userData.userId = res.entity.id
        
        wx.setStorage({
          key: 'login',
          data: param,
        })
        wx.setStorage({
          key: 'userInfo',
          data: res.entity,
          success: function (res) {
            //处理跳转逻辑
            util.redirect('/pages/index/index')
          }
        })
      }
    })
  },
  // 注册
  toRegister: function () {
    util.navigateTo( '/pages/register/register')
  },
  // 修改密码
  toPwd: function () {
    util.navigateTo('/pages/register/register?type=1&phone=' + this.data.phone)
  },
  //密码显示 切换
  showPwd: function () {
    var pwdFlag = !this.data.pwdFlag;
    console.log(pwdFlag);
    this.setData({
      pwdFlag: pwdFlag,
    })
  },
  // 输入
  inputListen: function (event) { 
    var key = event.currentTarget.dataset.key;
    console.log(event.detail.value)
    this.data[key] = event.detail.value;
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
    
  },
  onShow:function(){
    // 生命周期函数--监听页面显示
    
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
    
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
    
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
    
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
    
  },
  onShareAppMessage: function() {
    // 用户点击右上角分享
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  }
})
